<?php
class IndexController extends Zend_Controller_Action {
	
	public function init(){
		$init = new Model_Init($this);
	}
	
	public function indexAction() {
		//if user is login redirect to main page
		if (Zend_Auth::getInstance ()->hasIdentity ()) {
			$this->_helper->redirector ( Zend_Auth::getInstance ()->getIdentity ()->login, '' );
		}
		
		$form = new Form_Login ();
		if ($this->getRequest ()->isPost ()) {
			if ($form->isValid ( $this->getRequest ()->getPost () )) {
				$user = new Model_User ();
				if ($user->authorize ( $form->getValue ( 'login' ), $form->getValue ( 'password' ), $form->getValue ( 'rememberme' ) )) {
					//check if we don't have url from session, get if user go into page without autorizing
					$localSession = new Zend_Session_Namespace ( 'not_reg_view_url' );
					if (isset ( $localSession->user )) {
						$user_url = $localSession->user;
						//go to url like user/view/login/boo
						//TODO change it for adding some string to URL, maybe encoded in base64 and go to it
						$this->_helper->redirector ( '', $user_url, 'default', array ('login' => $user_url ) );
					} else {
						$this->_helper->redirector ( null, $form->getValue ( 'login' ) );
					}
				} else {
					$this->view->error = $this->view->translate ( "Login error" );
				}
			} else {
				$this->view->errors = $form->getMessages ();
			}
		}
		$this->view->form = $form;
		
	}
	
	/*
	 * show user info by his login in url
	 */
	public function mainAction() {
		$user_sign_data = Zend_Auth::getInstance ()->getIdentity ();
		$user_is_sign = Zend_Auth::getInstance ()->hasIdentity ();
		
		//check if user is sign in
		if (! $user_is_sign) {
			$this->_helper->redirector ( 'index', 'index' );
		}	
		
		//get user login from url
		$user_login = $this->_getParam ( 'login' );
		
		//check if login form url is current sing in user login
		if ($user_login === $user_sign_data->login) {
			$is_user_profile = true;
		} else {
			$is_user_profile = false;
		}
		
		if ($user_sign_data->login !== $user_login) {
			//search user by login
			//TODO create cache
			$modelUser = new Model_User();
			$user_model =  $modelUser->getUserInfoByLogin($user_login);
			
			//check if user exists
			if ($user_model) {
				$user = new Model_User ( $user_model->id );
				
				//add guests visit to page
				$guest = new Model_Guest();
				$guest->user_id = $user_model->id;
				$guest->friend_id  = Zend_Auth::getInstance()->getIdentity()->id;
				$guest->datetime = time();
				$guest->save();
				
				//and remove all previous visit of that user
				$guest->setVisited(Zend_Auth::getInstance()->getIdentity()->id, $user_model->id);
			} else {
				//TODO what to do, if there isn't any user
				echo $this->view->translate("There is no user");
			}
		} else {
			$user = new Model_User ( $user_sign_data->id );
		}
		
		//add user info to template
		$this->view->user = $user;
		$this->view->user_is_sign = $user_is_sign;
		$this->view->is_user_profile = $is_user_profile;
		$this->view->user_login = $user->login;
		$this->view->user_id = $user->id;
		
		//so bad to create two examples of class(((
		$user_guests = new Model_Guest();
		$this->view->guests = $user_guests->getGuestsListMain($user->id);
		
		
		//check if form getting $_post, in my way, form first login screen
		if ($this->getRequest ()->isPost ()) {

			//show form for first login
			$form = new Form_FirstLogin ();
			if ($form->isValid ( $this->getRequest ()->getPost () )) {
				$user->fill ( $form->getValues () );
				$user->first_login = 0;
				$user->save ();
				
				//create user folder
                mkdir("media_data".DIRECTORY_SEPARATOR.$user->login, 0755);
				
				$year_arr = array("visit_1992", "visit_1993","visit_1994","visit_1995","visit_1996","visit_1997","visit_1998","visit_1999",
				"visit_2000","visit_2001","visit_2002","visit_2003","visit_2004","visit_2005","visit_2006","visit_2007","visit_2008","visit_2009","visit_2010");
				
				//get years from visiting kazantip
				for($i = 1994; $i<=2010; $i++){
					$db_table = "visit_".$i;
					$year = $this->_getParam($db_table);
					
					//prevent diff table name
					if(isset($year) && in_array($db_table, $year_arr)){
						//write user_id to direct DB
						$model_visit = new Model_Visits($db_table);
						$model_visit->user_id = $user_sign_data->id;
						$model_visit->save();
					}
				}
			
				//update login, name, surname to SearchLucence
				$user_item = new Model_User( $user->id );
		
				$model = new Model_Search ( 'users' );
				$model->update_item( $user_item->id, $user_item->name, $user_item->surname, $user_item->login );
				
				//update info for user from fiiling passport on first login
				Zend_Auth::getInstance()->getIdentity()->name = $user_item->name;
				Zend_Auth::getInstance()->getIdentity()->surname = $user_item->surname;
				Zend_Auth::getInstance()->getIdentity()->login = $user_item->login;
			} else {
				$this->view->errors = $form->getMessages ();
			}
		}
		
		//unset value for redirecting if not authorize user view other user
		Zend_Session::namespaceUnset ( 'not_reg_view_url' );
	}
}

